
import urllib.request
import urllib.parse

# 百度关键词接口 http://www.baidu.com/s?wd=keyword
# 360关键词接口 http://www.so.com/s?q=keyword

baidu="http://www.baidu.com/s"

url = "http://www.so.com/s"
keyword=input('请输入搜索内容:')
word = {"q": keyword}
word = urllib.parse.urlencode(word)  # 编码成字符串
newurl = url+"?"+word  # 拼接网址
print(newurl)  #查看拼接网址
user_agent_list = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3969.0 Safari/537.36"
}

req = urllib.request.Request(newurl,headers=user_agent_list)    #添加请求头 ,添加搜索字段

# req.add_header("Connection", "keep-alive")

print(req.get_header('User-agent'))  #打印请求头  注意必须是第一个大写  其他的小写

response = urllib.request.urlopen(req,timeout=1)

print(response.getcode())  #返回响应状态码
print(response.geturl())  #返回实际数据的url 防止重定向问题
# print(response.info()) #返回相应报头

the_page = response.read().decode("utf8")
 
with open('baidu.html', 'w', encoding='utf-8') as f:
    f.write(the_page)



